package com.linkoog.devtools.code_generator.wizard.generate.database.table.dialog;

import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.custom.ColumnInfoJTable;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.ui.DocumentAdapter;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;

public class ColumnInfoDialog extends DialogWrapper {

    private JPanel myMainPanel;
    private JTextField tableNameJTextField;
    private JTextField entityNameJTextField;
    private JTextField commentJTextField;

    private JPanel tableSettingPanel;

    private JTable columnInfoTable;
    private JScrollPane columnInfoScrollPane;

    private TableInfo tableInfo;

    public ColumnInfoDialog(JComponent jComponent, TableInfo tableInfo) {
        super(jComponent, true);

        $$$setupUI$$$();
        setTitle(tableInfo.getTableName() + "表的字段信息");
        myMainPanel.setMinimumSize(new Dimension(670, 410));

        this.tableInfo = tableInfo;


        tableNameJTextField.setEditable(false);
        tableNameJTextField.setText(tableInfo.getTableName());
        entityNameJTextField.setText(tableInfo.getJavaEntityName());
        commentJTextField.setText(tableInfo.getTableComment());

        entityNameJTextField.getDocument().addDocumentListener(new DocumentAdapter() {
            @Override
            protected void textChanged(@NotNull DocumentEvent e) {
                Document doc = e.getDocument();
                try {
                    if (doc.getLength() > 0) {
                        String javaEntityName = doc.getText(0, doc.getLength());
                        tableInfo.setJavaEntityName(javaEntityName);
                    }
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
            }
        });

        getColumnInfoTable().getTableInfoModel().reset(tableInfo.getColumnInfos());
        init();
    }


    @Override
    protected @Nullable ValidationInfo doValidate() {
        return super.doValidate();
    }

    @Override
    public void doCancelAction() {
        super.doCancelAction();
    }

    @Override
    protected void doOKAction() {
        super.doOKAction();
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        return myMainPanel;
    }

    public ColumnInfoJTable getColumnInfoTable() {
        return (ColumnInfoJTable) columnInfoTable;
    }

    private void createUIComponents() {
        //TableSettingPanelBuilder tableSettingPanel = new TableSettingPanelBuilder();
        //this.tableSettingPanel = tableSettingPanel.getMyMainPanel();
        //this.tableSettingPanel.setBorder(new TitledBorder(new LineBorder(new Color(196, 196, 196)), "对当前表有效"));

        columnInfoTable = new ColumnInfoJTable();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new GridLayoutManager(4, 4, new Insets(0, 0, 0, 0), -1, -1));
        columnInfoScrollPane = new JScrollPane();
        myMainPanel.add(columnInfoScrollPane, new GridConstraints(3, 0, 1, 4, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        columnInfoScrollPane.setViewportView(columnInfoTable);
        tableSettingPanel = new JPanel();
        tableSettingPanel.setLayout(new BorderLayout(0, 0));
        myMainPanel.add(tableSettingPanel, new GridConstraints(2, 0, 1, 4, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JLabel label1 = new JLabel();
        label1.setText("表名");
        myMainPanel.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        tableNameJTextField = new JTextField();
        myMainPanel.add(tableNameJTextField, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("类名");
        myMainPanel.add(label2, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        entityNameJTextField = new JTextField();
        myMainPanel.add(entityNameJTextField, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label3 = new JLabel();
        label3.setText("注释");
        myMainPanel.add(label3, new GridConstraints(1, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        commentJTextField = new JTextField();
        myMainPanel.add(commentJTextField, new GridConstraints(1, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }

}
